Automating System Backups With cron and zip

Scot Hacker, 2/99

Backups are a great idea -- if you remember to run them. Make things easy on yourself by establishing an automated backup system to run at regular intervals, possibly in the middle of the night. There are official backup utilities available for BeOS, but everything you need to safeguard your data is free: good old zip, the Unix cron utility, and a simple bash shell script. This package outlines an automated system backup procedure and includes sample crontab file and backup scripts.

For testing purposes, this package will back up your ~/people and ~/queries directories once every minute. The instructions below will tell you how to modify the sample script and crontab file to back up other data, and at more reasonable intervals.

In case you're not familiar with it, cron is a common Unix utility that runs in the background, waiting for pre-specified time intervals to roll around, as specified in an associated "crontab" file. When those times arrive, associated commands are run. Since those commands can be scripts, we're going to tell cron to invoke a script at specified intervals. That script contains the backup commands we want to run.

1) Download the cron utility from http://www.be.com/beware/Utilities/cron.html. Move its cron and crontab binaries to /boot/home/config/bin

2) From this package, move the file backup.sh to /boot/home/config/bin

3) From this package, move the file cronjobs to /boot/home/config/etc

4) Create a folder called backups in the root of your boot drive (or anywhere you want; just edit backup.sh to reflect the actual location).

5) Open a Terminal window and type these two lines:

cron
crontab ~/config/etc/cronjobs
6) cron is now running in the background, using the cronjobs file to determine it's schedule. In this sample distribution, your people and queries folders will be backed up once every minute. Each time they're backed up, report lines will be written to a text file: /boot/backups/backup.log and an alert box will appear on your screen informing you that they were created. This is so you'll remember to move the new archives to a different disk volume, since backups don't do you any good if they're living on the same volume you just backed up. Of course, you can easily modify the script to create the archives on a different volume to begin with.

After the alert box appears, take a look in /boot/backups to find the new archives and backup.log. Once you're satisfied everything is working properly, kill cron (because it's annoying to have it running every minute) by typing:

kill -9 cron
Since you probably want to back up more than just your people and queries directories, you'll need to modify the script to backup what you really want saved (probably your home and apps hierarchies) and modify the cronjobs file to run at a more realistic interval.

7) Open /boot/home/config/bin/backup.sh in a text editor and replace the paths to people and queries with the paths to the actual files or directories you want archived. Make sure you catch all references. The lines that run the actual zip commands are very simple in this demo script, but zip is a very powerful backup tool, and can take tons of flags to modify its behavior. See http://www.birdhouse.org/beos/tips/archive/tip188.html for an explanation of zip's most important capabilities (or buy a copy of the BeOS Bible for more details).

8) Now you need to modify the cronjobs file to run at the desired interval. Open /boot/home/config/etc/cronjobs (also known as your crontab file) in a text editor. You'll see this:

* * * * * ~/config/bin/backup.sh

Note that you must specify the full path to the command you want to run!

The format of a crontab file is:

[schedule] [command]

The command part is obvious, but the schedule part can be a little tricky if you haven't worked with cron before. You've got five time specifiers, separated by spaces, and represented here by wildcards, since we're running every minute. The fields are:

(1) minute, (2) hours, (3)day-of-month, (4) month, (5) weekday

More specifically,
Field Meaning Range
minutes minutes after the hour 0-59
hours hour of the day 0-23 (0=midnight)
day-of-the-month numeric day within a month 1-31
month month of the year 1-12
weekday day of the week 0-6 (0=Sunday)
Thanks to http://acit.cgu.edu/acit/help/cron.html for the table above.

So, for example, the following crontab line:

58 16 30 4 4 ~/config/bin/backup.sh
would start the job at 4:58 p.m. on Thursday-30-April. But we need to get more regular than that. Let's say you want to run your job just after midnight, every two weeks. We'll schedule it for 12:01 a.m. on the 1st and 15th of every month:
01 00 1,15 * * ~/config/bin/backup.sh
Note that any field can be specified as a range (e.g. 1-19) or as a series (e.g. 0,3,5). The last two units are left as wildcards since we don't care what month or day it is. Paste the line above into your cronjobs file, save it, restart cron, and you're on you're way.

9) Now all that remains is to add these two lines to your UserBootscript so that cron will always be running.

cron
crontab ~/config/etc/cronjobs
That's it!